// const http = require('http');


// const server = http.createServer((req, res) => {

//     res.writeHead(200, { 'Content-Type': 'text/plain' });

//     if (req.url === '/') {
//         res.end('../html/js.html');
//     } else if (req.url === '/about') {
//         res.end('This is the about page.\n');
//     } else {
//         res.writeHead(404, { 'Content-Type': 'text/plain' });
//         res.end('Page not found\n');
//     }
// });




const http = require('http');
const fs = require('fs');
const path = require('path');

const server = http.createServer(async (req, res) => {
    try {
        let filePath;
        
        if (req.url === '/') {
            filePath = path.join(__dirname, './js.html');
        }
        else {
            res.writeHead(404, { 'Content-Type': 'text/html' });
            return res.end('<h1>404 Not Found</h1>');
        }
        
        const content = await fs.readFile(filePath, 'utf8');
        res.writeHead(200, { 'Content-Type': 'text/html' });
        res.end(content);
        
    } catch (err) {
        console.error(err);
        res.writeHead(500, { 'Content-Type': 'text/plain' });
        res.end('Internal Server Error');
    }
});

const PORT = 3000;
const HOST = 'localhost';


server.listen(PORT, HOST, () => {
    console.log(`Server running at http://${HOST}:${PORT}/`);
});